import 'dart:math';
import 'dart:ui';

import 'package:animation_effect_demo/src/effects/effect_template.dart';

import '../base/base_effect.dart';
import 'image_animation_effect.dart';
import 'image_operation.dart';

class StaticImageAnimationEffect extends ImageAnimationEffect {
  @override
  EffectType get effectTyp => EffectType.staticImageEffect;

  @override
  int get frameDuration => 50;

  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) async {
    ImageOperation imageOperation = operation!.imageOperation!;
    List<String> imageList = imageOperation.imagePaths;

    int sizeWidth = size.width.toInt();
    int sizeHeight = size.height.toInt();

    int imageWidth = imageOperation.width;
    int imageHeight = imageOperation.height;

    double displayRatio = sizeWidth / sizeHeight;

    double imageRatio = imageWidth / imageHeight;

    int targetWidth;
    int targetHeight;

    if (imageOperation.fixType == FixType.fill) {
      targetWidth = sizeWidth;
      targetHeight = sizeHeight;
    } else if (imageOperation.fixType == FixType.contain) {
      var sideLength = min(sizeWidth, sizeHeight);
      targetWidth = sideLength;
      targetHeight = sideLength;
    } else if (imageOperation.fixType == FixType.cover) {
      if (displayRatio < imageRatio) {
        targetHeight = sizeHeight;
        targetWidth = (imageRatio * sizeHeight).ceil();
      } else if (displayRatio > imageRatio) {
        targetHeight = (sizeWidth / imageRatio).ceil();
        targetWidth = sizeWidth;
      } else {
        targetHeight = sizeHeight;
        targetWidth = sizeWidth;
      }
    } else {
      targetWidth = (imageWidth * 0.2).toInt();
      targetHeight = (imageHeight * 0.2).toInt();
    }


    final imageFrame = await getFrameByImagePath(imageList.first,
        fixType: imageOperation.fixType,
        width: targetWidth,
        height: targetHeight,
        cropWidth: sizeWidth,
        cropHeight: sizeHeight);

    return [imageFrame];
  }

  @override
  Size get size => const Size(24, 24);

}